package com.bjpowernode.utils.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bjpowernode.config.context.ApplicationContextProvider;
import com.bjpowernode.constant.Constants;
import com.bjpowernode.service.TDicValueService;
import jakarta.annotation.Resource;

import java.util.Map;

/**
 *
 */
public class SourceConverter implements Converter<Integer> {

    @Resource
    private TDicValueService tDicValueService;

    @Override
    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 获取spring的bean
        TDicValueService tDicValueService = ApplicationContextProvider
                .getApplicationContext()
                .getBean(TDicValueService.class);
        // 调用方法 ,获取全部数据
        Map<String, Integer> stringIntegerMap = tDicValueService.selectAllNameAndIdByClueState(Constants.SOURCE);
        // 查询转换数据
        String stringValue = cellData.getStringValue();
        // 转换赋值
        return stringIntegerMap.get(stringValue);
    }

    @Override
    public WriteCellData<?> convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == 17) {
            return new WriteCellData<>("视频直播");
        } else {
            return new WriteCellData<>("车展会");
        }
        /*Map<Integer, String> integerStringMap = tDicValueService.selectAllIdAndNameByTypeCode("source");
        String s = integerStringMap.get(value);
        return new WriteCellData<>(s);*/
    }
}
